{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import tushare as ts\n",
    "import pandas as pd\n",
    "from plotly.graph_objs import *\n",
    "import os\n",
    "ts.set_token('ac8805887aa21d24fd5ec47a4ae9e7a09804ebaeedea93352a7b13d2e8f08e46')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[Getting data:]##############################################################################"
     ]
    }
   ],
   "source": [
    "STOCKS=[\n",
    "    \"000507\", #珠海港\n",
    "    \"002497\", #  雅化集团\n",
    "    \"000877\", #  天山股份\n",
    "    \"002302\", #  西部建设\n",
    "    \"002307\"  # 北新路桥\n",
    "    \n",
    "]\n",
    "DATA_FILE_PATH = '..\\\\data\\\\'\n",
    "df = ts.get_today_ticks('000507', 5, 0.001)\n",
    "#df.size()\n",
    "#print(df.head(5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "..\\data\\000507_170710_ticks.xlsx is saved.\n"
     ]
    }
   ],
   "source": [
    "\n",
    "file = DATA_FILE_PATH + '000507_170710_ticks.xlsx'\n",
    "if os.path.exists(file):\n",
    "    print(file+ \" is exist.\")\n",
    "else:\n",
    "    print(file + \" is saved.\")\n",
    "    df.to_excel(file)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "..\\data\\STOCKS_170710realtime_quotes.xlsx is created and saved.\n",
      "..\\data\\STOCKS_170710realtime_quotes.xlsx\n"
     ]
    }
   ],
   "source": [
    "#budget = pd.read_csv(r'D:\\Source\\PythonProjects\\learning_tushare_tools\\data\\000507_today_ticks.xlsx')\n",
    "# 这里读取不成功   \n",
    "stock_code = '000507'\n",
    "df_realtiome_quo = ts.get_realtime_quotes(STOCKS)\n",
    "file = '..\\\\data\\\\STOCKS_170710realtime_quotes.xlsx'\n",
    "if os.path.exists(file):\n",
    "     print(file+ \" is exist.\")\n",
    "else:\n",
    "    df_realtiome_quo.to_excel(file)\n",
    "    print(file+ \" is created and saved.\")\n",
    "print(file)\n",
    "df_rq2 = df_realtiome_quo[['pre_close', 'open','low','high']]\n",
    "#df_rq2['high_low'] = [float(df_rq2['high']) - float(df_rq2['low'])]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "..\\data\\000507_170710hist_data.xlsx is saved.\n"
     ]
    }
   ],
   "source": [
    "start1 = '2017-05-01'\n",
    "end1 = '2017-07-07'\n",
    "stock_code = '000507'\n",
    "df_hist_data = ts.get_hist_data(stock_code,start=start1,end=end1)\n",
    "\n",
    "file = DATA_FILE_PATH+stock_code+\"_170710hist_data.xlsx\"\n",
    "if os.path.exists(file):\n",
    "    print(file + ' is exist.')\n",
    "else:\n",
    "    df_hist_data.to_excel(file)\n",
    "    print(file + ' is saved.')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             open   high  close   low     volume  price_change  p_change\n",
      "date                                                                    \n",
      "2017-07-07  12.26  12.37  12.13  12.1  574877.50         -0.25     -2.02\n",
      "2017-07-06  12.44  12.64  12.38  12.2  742240.81         -0.06     -0.48\n"
     ]
    }
   ],
   "source": [
    "print(df_hist_data[['open','high','close','low','volume','price_change', 'p_change']].head(2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "             open   high  close    low  price_change  p_change  open-low\n",
      "date                                                                    \n",
      "2017-07-05  12.28  12.56  12.44  12.16          0.24      1.97      0.12\n",
      "2017-06-27  12.90  13.88  13.48  12.90          0.59      4.58      0.00\n",
      "2017-06-23  12.46  13.10  12.94  12.01          0.48      3.85      0.45\n",
      "2017-06-16  12.63  13.97  13.33  12.51          0.60      4.71      0.12\n",
      "2017-06-13  12.63  13.45  13.45  12.42          1.22      9.97      0.21\n",
      "2017-06-08  11.62  12.46  12.46  11.56          1.13      9.97      0.06\n",
      "2017-06-07  10.34  11.35  11.35  10.21          1.03      9.98      0.13\n",
      "2017-06-06   9.81  10.55  10.32   9.76          0.41      4.14      0.05\n",
      "2017-06-02   9.33   9.98   9.88   9.11          0.30      3.13      0.22\n",
      "2017-05-31   9.84  10.18  10.00   9.60          0.33      3.41      0.24\n",
      "2017-05-24   8.96   9.86   9.82   8.58          0.86      9.60      0.38\n",
      "2017-05-16   9.74  10.21  10.16   9.40          0.29      2.94      0.34\n",
      "2017-05-12   8.74   9.69   9.69   8.63          0.88      9.99      0.11\n",
      "2017-05-11   8.34   8.95   8.81   8.05          0.50      6.02      0.29\n",
      "2017-05-03   9.81  10.55  10.20   9.64          0.39      3.98      0.17\n",
      "2017-05-02   9.60   9.98   9.81   9.51          0.22      2.29      0.09\n"
     ]
    }
   ],
   "source": [
    "# 获取收盘价比昨天收盘价高0.1以上，并且开盘价低于收盘价的日期；\n",
    "df_price_change_list = df_hist_data[\n",
    "    (df_hist_data['price_change'] > 0.2)\n",
    "    &(df_hist_data['open'] < df_hist_data['close'])\n",
    "]\n",
    "# 这里定义一个函数，可以应用到每一行，设置axis=1就是应用到每一行的意思；如果设置axis=0，就是应用于每一列的意思\n",
    "def openMinusLow(x):\n",
    "    return float(x['open'])-float(x['low'])\n",
    "#df_price_change_list['open-low'] = float(df_price_change_list['open'])-float(df_price_change_list['low'])\n",
    "df_temp = df_price_change_list.apply(openMinusLow, axis=1)\n",
    "# 用insert就没有SettingWithCopyWarning警告了\n",
    "df_price_change_list.insert(0,'open-low',df_temp)\n",
    "print(df_price_change_list[['open', 'high', 'close', 'low', 'price_change', 'p_change','open-low']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [],
   "source": [
    "import datetime\n",
    "def get_stock_name(stock_code):\n",
    "    df = ts.get_realtime_quotes(stock_code)\n",
    "    return df['name'][0]\n",
    "def get_stock_operate_date(stock_code):\n",
    "    def openMinusLow(x):\n",
    "        return float(x['open'])-float(x['low'])\n",
    "    #def get_cycle(x)\n",
    "        '''想做一个获取当前可操作日和上一天可操作日的时间差'''\n",
    "    print('统计股票：'+stock_code\n",
    "          +'(' + get_stock_name(stock_code) + ')'\n",
    "          +'\\n* 当天上涨率高于0.2的天数。')\n",
    "    print('* 说明：open-low表示开盘价和最低价相减的结果，越接近说明开盘买入基本上就是最低价')\n",
    "    print('        然后在看price_change，高于0.3以上，400股就能赚100块以上')\n",
    "    print(         'price_change=收盘价-昨收价，p_change=price_change/昨收价*100%')\n",
    "    print('-------------------------------------------------------------------------------')\n",
    "    start0 = '2017-05-02'\n",
    "    end0 = str(datetime.date.today())\n",
    "    #print('start = '+start0)\n",
    "    #print('end = '+end0)\n",
    "    #print(stock_code)\n",
    "    df_h_data = ts.get_hist_data(stock_code,start=start0,end=end0)\n",
    "    # print(df_h_data[['open','high','close','low','volume','price_change', 'p_change']].head(2))\n",
    "    # 获取收盘价比昨天收盘价高0.1以上，并且开盘价低于收盘价的日期；\n",
    "    df_price_change_list = df_h_data[\n",
    "        (df_h_data['price_change'] > 0.2)\n",
    "        &(df_h_data['open'] < df_h_data['close'])\n",
    "    ]\n",
    "    df_temp = df_price_change_list.apply(openMinusLow, axis=1)\n",
    "    # 用insert就没有SettingWithCopyWarning警告了\n",
    "    df_price_change_list.insert(0,'open-low',df_temp)\n",
    "    print(df_price_change_list[['open', 'high', 'close', 'low', 'price_change', 'p_change','open-low']].head(2))\n",
    "    return df_price_change_list\n",
    "\n",
    "#df_stock_operate1 = get_stock_operate_date('002497')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "统计股票：000877(天山股份)\n",
      "* 当天上涨率高于0.2的天数。\n",
      "* 说明：open-low表示开盘价和最低价相减的结果，越接近说明开盘买入基本上就是最低价\n",
      "        然后在看price_change，高于0.3以上，400股就能赚100块以上\n",
      "price_change=收盘价-昨收价，p_change=price_change/昨收价*100%\n",
      "-------------------------------------------------------------------------------\n",
      "             open   high  close    low  price_change  p_change  open-low\n",
      "date                                                                    \n",
      "2017-07-03  12.77  13.94  13.94  12.68          1.27     10.02      0.09\n",
      "2017-06-28  12.49  13.50  13.30  12.24          0.82      6.57      0.25\n"
     ]
    }
   ],
   "source": [
    "df_stock_operate2 = get_stock_operate_date('000877')#天山股份\n",
    "# 天山股票这个股票开盘价和最低价之间还有较大的差异，不太好开盘就买入，需要具体分析最低价格出现的时间\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "统计股票：002302(西部建设)\n",
      "* 当天上涨率高于0.2的天数。\n",
      "* 说明：open-low表示开盘价和最低价相减的结果，越接近说明开盘买入基本上就是最低价\n",
      "        然后在看price_change，高于0.3以上，400股就能赚100块以上\n",
      "price_change=收盘价-昨收价，p_change=price_change/昨收价*100%\n",
      "-------------------------------------------------------------------------------\n",
      "             open   high  close    low  price_change  p_change  open-low\n",
      "date                                                                    \n",
      "2017-07-05  22.82  24.88  23.92  22.82          0.80      3.46      0.00\n",
      "2017-07-03  22.33  23.92  23.73  21.62          1.56      7.04      0.71\n"
     ]
    }
   ],
   "source": [
    "df_stock_operate3 = get_stock_operate_date('002302')# 西部建设"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "统计股票：002307(北新路桥)\n",
      "* 当天上涨率高于0.2的天数。\n",
      "* 说明：open-low表示开盘价和最低价相减的结果，越接近说明开盘买入基本上就是最低价\n",
      "        然后在看price_change，高于0.3以上，400股就能赚100块以上\n",
      "price_change=收盘价-昨收价，p_change=price_change/昨收价*100%\n",
      "-------------------------------------------------------------------------------\n",
      "             open   high  close    low  price_change  p_change  open-low\n",
      "date                                                                    \n",
      "2017-07-06  18.90  19.64  19.38  18.73          0.39      2.05      0.17\n",
      "2017-07-05  18.69  19.43  18.99  18.66          0.33      1.77      0.03\n"
     ]
    }
   ],
   "source": [
    "df_stock_operate4 = get_stock_operate_date('002307')#北新路桥002307"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------Data: 2017-07-06--------------------\n",
      "--------------------LowestPrice :18.73---------------------\n",
      "       time  price change  volume    amount type\n",
      "0  15:00:03  19.38     --   15928  30868560   卖盘\n",
      "1  14:57:00  19.38   0.01     159    308142   买盘\n",
      "...... total: 4645\n",
      "--->从 09:39:09 到 09:42:12有合适的时间点：18个\n",
      "          time  price change  volume   amount type\n",
      "4399  09:42:12  18.77  -0.01     543  1019211   卖盘\n",
      "4404  09:41:57  18.77  -0.01     250   469250   卖盘\n",
      "4407  09:41:48  18.77  -0.01     259   486143   卖盘\n",
      "4446  09:39:51  18.77     --     358   671966   卖盘\n",
      "4447  09:39:48  18.77     --     100   187700   卖盘\n",
      "4448  09:39:45  18.77     --     195   366015   卖盘\n",
      "4449  09:39:42  18.77   0.01     358   671966   买盘\n",
      "4450  09:39:39  18.76     --     493   924868   卖盘\n",
      "4451  09:39:36  18.76     --     149   279524   卖盘\n",
      "4452  09:39:33  18.76     --     179   335804   卖盘\n",
      "4453  09:39:30  18.76   0.01     354   664104   买盘\n",
      "4454  09:39:27  18.75  -0.01     423   793125   卖盘\n",
      "4455  09:39:24  18.76   0.01     315   590940   买盘\n",
      "4456  09:39:21  18.75  -0.02     341   639375   卖盘\n",
      "4457  09:39:18  18.77     --     330   619410   买盘\n",
      "4458  09:39:15  18.77     --     333   625041   买盘\n",
      "4459  09:39:12  18.77     --     194   364138   卖盘\n",
      "4460  09:39:09  18.77  -0.02     338   634426   卖盘\n",
      "--------------------Data: 2017-07-05--------------------\n",
      "--------------------LowestPrice :18.66---------------------\n",
      "       time  price change  volume    amount type\n",
      "0  15:00:03  18.99     --    8724  16568034   买盘\n",
      "1  14:57:00  18.99     --     226    429173   买盘\n",
      "...... total: 4612\n",
      "--->从 09:25:03 到 09:31:06有合适的时间点：23个\n",
      "          time  price change  volume  amount type\n",
      "4589  09:31:06  18.70     --     317  592790   卖盘\n",
      "4590  09:31:03  18.70     --      61  114070   买盘\n",
      "4591  09:31:00  18.70     --       7   13090   买盘\n",
      "4592  09:30:57  18.70     --      34   63580   买盘\n",
      "4593  09:30:54  18.70     --      65  121550   买盘\n",
      "4594  09:30:51  18.70   0.01       7   13090   买盘\n",
      "4595  09:30:48  18.69     --      81  151389   买盘\n",
      "4596  09:30:45  18.69     --      22   41118   卖盘\n",
      "4597  09:30:42  18.69     --      38   71022   买盘\n",
      "4598  09:30:39  18.69     --      28   52332   买盘\n",
      "4599  09:30:36  18.69  -0.01      53   99057   卖盘\n",
      "4600  09:30:33  18.70   0.01      33   61710   买盘\n",
      "4601  09:30:30  18.69     --       3    5607   卖盘\n",
      "4602  09:30:27  18.69     --     157  293433   买盘\n",
      "4603  09:30:24  18.69     --       6   11214   买盘\n",
      "4604  09:30:21  18.69   0.01     103  192507   买盘\n",
      "4605  09:30:15  18.68  -0.01      23   42964  中性盘\n",
      "4606  09:30:12  18.69     --      55  102795   买盘\n",
      "4607  09:30:09  18.69     --      30   56070   买盘\n",
      "4608  09:30:06  18.69  -0.01     204  381276   卖盘\n",
      "4609  09:30:03  18.70   0.01     516  964920   买盘\n",
      "4610  09:30:00  18.69     --       0       0   卖盘\n",
      "4611  09:25:03  18.69  18.69     357  667251   买盘\n",
      "--------------------Data: 2017-07-03--------------------\n",
      "--------------------LowestPrice :18.01---------------------\n",
      "       time  price change  volume    amount type\n",
      "0  15:00:03  19.01     --   16122  30647922   卖盘\n",
      "1  14:57:00  19.01  -0.01     426    809826   卖盘\n",
      "...... total: 4430\n",
      "--->从 09:55:36 到 09:56:48有合适的时间点：23个\n",
      "          time  price change  volume  amount type\n",
      "3904  09:56:48  18.05  -0.01      64  115520   卖盘\n",
      "3905  09:56:45  18.06   0.01      95  171570   买盘\n",
      "3906  09:56:42  18.05     --       7   12635   卖盘\n",
      "3907  09:56:39  18.05     --      28   50540   卖盘\n",
      "3908  09:56:36  18.05     --      68  122740   买盘\n",
      "3909  09:56:33  18.05   0.01     108  194940   买盘\n",
      "3910  09:56:30  18.04     --     150  270600   卖盘\n",
      "3911  09:56:27  18.04   0.02      46   82984   买盘\n",
      "3912  09:56:24  18.02     --      38   68476   卖盘\n",
      "3913  09:56:21  18.02  -0.02      78  140556   卖盘\n",
      "3914  09:56:18  18.04   0.03     123  221892   买盘\n",
      "3915  09:56:15  18.01  -0.01     185  333185   卖盘\n",
      "3916  09:56:12  18.02   0.01      83  149566   买盘\n",
      "3917  09:56:09  18.01     --     145  261145   卖盘\n",
      "3918  09:56:06  18.01     --     212  381812   卖盘\n",
      "3919  09:56:03  18.01  -0.01      78  140478   卖盘\n",
      "3920  09:56:00  18.02  -0.03     319  574838   卖盘\n",
      "3921  09:55:57  18.05  -0.01     176  317680   卖盘\n",
      "3922  09:55:54  18.06     --     171  308826   买盘\n",
      "3923  09:55:51  18.06     --     111  200466   买盘\n",
      "3924  09:55:48  18.06  -0.02      39   70434   买盘\n",
      "3926  09:55:42  18.06  -0.01     194  350364   卖盘\n",
      "3928  09:55:36  18.06  -0.01      96  173376   卖盘\n",
      "--------------------Data: 2017-06-28--------------------\n",
      "--------------------LowestPrice :17.71---------------------\n",
      "       time  price change  volume    amount type\n",
      "0  15:00:03  19.08   0.01   16963  32365403   卖盘\n",
      "1  14:57:00  19.07  -0.01     379    722753   卖盘\n",
      "...... total: 4629\n",
      "--->从 09:47:09 到 10:12:45有合适的时间点：24个\n",
      "          time  price change  volume   amount type\n",
      "3792  10:12:45  17.75   0.01      10    17750   卖盘\n",
      "3793  10:12:42  17.74     --     307   544618   买盘\n",
      "3794  10:12:39  17.74     --     109   193365   买盘\n",
      "3795  10:12:36  17.74     --      38    67412   买盘\n",
      "3796  10:12:33  17.74   0.02     323   573002   买盘\n",
      "3797  10:12:30  17.72     --     225   398700   买盘\n",
      "3798  10:12:27  17.72     --     147   260483   卖盘\n",
      "3799  10:12:24  17.72     --     180   318960   卖盘\n",
      "3800  10:12:21  17.72     --      84   148848   卖盘\n",
      "3801  10:12:18  17.72   0.01      78   138216   买盘\n",
      "3802  10:12:15  17.71  -0.01      59   104489   卖盘\n",
      "3803  10:12:12  17.72   0.01     170   301240   卖盘\n",
      "3804  10:12:09  17.71  -0.01     197   348887   卖盘\n",
      "3805  10:12:06  17.72   0.01     471   834612  中性盘\n",
      "3806  10:12:03  17.71  -0.03      15    26565   卖盘\n",
      "3807  10:12:00  17.74   0.01      38    67412   买盘\n",
      "3808  10:11:57  17.73  -0.01     933  1654209   卖盘\n",
      "3809  10:11:54  17.74  -0.01     193   342381   卖盘\n",
      "3810  10:11:51  17.75     --      70   124250   买盘\n",
      "3811  10:11:48  17.75     --      22    39050   买盘\n",
      "3812  10:11:45  17.75  -0.01     457   811175   卖盘\n",
      "3814  10:11:39  17.75  -0.01     787  1396925   卖盘\n",
      "4285  09:47:15  17.75  -0.01     138   244950   卖盘\n",
      "4287  09:47:09  17.75  -0.01     258   457950   卖盘\n",
      "--------------------Data: 2017-06-21--------------------\n",
      "--------------------LowestPrice :17.91---------------------\n",
      "       time  price change  volume    amount type\n",
      "0  15:00:03  19.49     --   22793  44423557   买盘\n",
      "1  14:57:00  19.49   0.04     462    900437   买盘\n",
      "...... total: 4688\n",
      "--->从 09:33:48 到 09:34:09有合适的时间点：6个\n",
      "          time  price change  volume   amount type\n",
      "4603  09:34:09  17.95  -0.03     918  1647810   卖盘\n",
      "4605  09:34:03  17.95     --     833  1495235   买盘\n",
      "4606  09:34:00  17.95   0.03     765  1373175   买盘\n",
      "4607  09:33:57  17.92  -0.05     812  1455104   卖盘\n",
      "4609  09:33:51  17.95     --     242   434390   卖盘\n",
      "4610  09:33:48  17.95  -0.02     179   321305   卖盘\n",
      "--------------------Data: 2017-06-19--------------------\n",
      "--------------------LowestPrice :17.81---------------------\n",
      "       time  price change  volume    amount type\n",
      "0  15:00:03   19.2     --   24647  47322240   卖盘\n",
      "1  14:57:00   19.2     --     549   1054080   卖盘\n",
      "...... total: 4617\n",
      "--->从 09:31:57 到 10:48:09有合适的时间点：81个\n",
      "          time  price change  volume   amount type\n",
      "3061  10:48:09  17.85  -0.01     114   203490   卖盘\n",
      "3063  10:48:03  17.85  -0.02      31    55335   卖盘\n",
      "3065  10:47:57  17.85  -0.01     333   594405   卖盘\n",
      "3067  10:47:51  17.85  -0.01       3     5355   卖盘\n",
      "3134  10:44:27  17.85  -0.01      21    37485   卖盘\n",
      "3136  10:44:21  17.84  -0.01     120   214080   卖盘\n",
      "3137  10:44:18  17.85     --      74   132090   买盘\n",
      "3138  10:44:15  17.85     --      53    94605   买盘\n",
      "3139  10:44:12  17.85   0.01      81   144585   买盘\n",
      "3140  10:44:09  17.84  -0.01     100   178400   卖盘\n",
      "3141  10:44:06  17.85   0.01     439   783615   买盘\n",
      "3142  10:44:03  17.84   0.01      21    37464  中性盘\n",
      "3143  10:44:00  17.83  -0.01      78   139074   卖盘\n",
      "3144  10:43:57  17.84   0.01      43    76712   买盘\n",
      "3145  10:43:54  17.83     --      36    64187   卖盘\n",
      "3146  10:43:51  17.83     --      39    69537   买盘\n",
      "3147  10:43:48  17.83   0.01     262   467145   买盘\n",
      "3148  10:43:45  17.82  -0.01      33    58806   卖盘\n",
      "3149  10:43:42  17.83     --     113   201478   买盘\n",
      "3150  10:43:39  17.83   0.01     133   237138   买盘\n",
      "3151  10:43:36  17.82  -0.01     118   210276   卖盘\n",
      "3152  10:43:33  17.83   0.02     202   360165   买盘\n",
      "3153  10:43:30  17.81     --     399   710619   卖盘\n",
      "3154  10:43:27  17.81  -0.01     231   411410   卖盘\n",
      "3155  10:43:24  17.82     --     192   342144   买盘\n",
      "3156  10:43:21  17.82   0.01     336   598752   买盘\n",
      "3157  10:43:18  17.81     --     114   203033   卖盘\n",
      "3158  10:43:15  17.81     --     272   484431   卖盘\n",
      "3159  10:43:12  17.81  -0.01     159   283179   卖盘\n",
      "3160  10:43:09  17.82   0.01     143   254826   买盘\n",
      "...        ...    ...    ...     ...      ...  ...\n",
      "3182  10:42:03  17.84  -0.01      70   124880   卖盘\n",
      "3183  10:42:00  17.85     --      96   171360   买盘\n",
      "3184  10:41:57  17.85     --      94   167790   卖盘\n",
      "3185  10:41:54  17.85  -0.01     351   626535   卖盘\n",
      "3188  10:41:45  17.85  -0.01      53    94605   卖盘\n",
      "4144  09:53:33  17.85     --     243   433755   买盘\n",
      "4145  09:53:30  17.85  -0.01    1298  2316930   卖盘\n",
      "4148  09:53:21  17.85     --     364   649740   卖盘\n",
      "4149  09:53:18  17.85   0.02     424   756840   买盘\n",
      "4150  09:53:15  17.83     --     407   725680   卖盘\n",
      "4151  09:53:12  17.83     --     187   333420   卖盘\n",
      "4152  09:53:09  17.83     --     406   723897   买盘\n",
      "4153  09:53:06  17.83     --     345   615134   买盘\n",
      "4154  09:53:03  17.83     --     408   727463   买盘\n",
      "4155  09:53:00  17.83  -0.01     387   690020   卖盘\n",
      "4156  09:52:57  17.84   0.01     195   347880  中性盘\n",
      "4157  09:52:54  17.83  -0.01     504   898631   卖盘\n",
      "4158  09:52:51  17.84     --     209   372856   买盘\n",
      "4159  09:52:48  17.84     --     857  1528888   买盘\n",
      "4160  09:52:45  17.84     --     288   513792   买盘\n",
      "4161  09:52:42  17.84  -0.01     348   620832   卖盘\n",
      "4162  09:52:39  17.85     --     393   701505   买盘\n",
      "4163  09:52:36  17.85     --     256   456960   买盘\n",
      "4164  09:52:33  17.85  -0.01     182   324870   卖盘\n",
      "4167  09:52:24  17.85  -0.01     459   819315   卖盘\n",
      "4572  09:32:09  17.85   0.02     296   528360  中性盘\n",
      "4573  09:32:06  17.83  -0.01     468   834443  中性盘\n",
      "4574  09:32:03  17.84   0.01     241   429944   买盘\n",
      "4575  09:32:00  17.83  -0.02     156   278148   卖盘\n",
      "4576  09:31:57  17.85  -0.01     252   449820   卖盘\n",
      "\n",
      "[81 rows x 6 columns]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------Data: 2017-06-16--------------------\n",
      "--------------------LowestPrice :17.4---------------------\n",
      "       time  price change  volume    amount type\n",
      "0  15:00:03  18.30  -0.03   18378  33631740   卖盘\n",
      "1  14:57:00  18.33   0.02     627   1149291   买盘\n",
      "...... total: 4699\n",
      "--->从 09:44:48 到 09:48:51有合适的时间点：22个\n",
      "          time  price change  volume   amount type\n",
      "4320  09:48:51  17.43     --     315   549045   买盘\n",
      "4321  09:48:48  17.43  -0.01     426   742518   卖盘\n",
      "4322  09:48:45  17.44     --     472   823168   买盘\n",
      "4323  09:48:42  17.44   0.02     377   657488   买盘\n",
      "4324  09:48:39  17.42  -0.02     532   926744   卖盘\n",
      "4325  09:48:36  17.44  -0.01     925  1613200  中性盘\n",
      "4386  09:45:33  17.44   0.01     403   702832   买盘\n",
      "4387  09:45:30  17.43     --     433   754719   买盘\n",
      "4388  09:45:27  17.43     --     632  1101576   买盘\n",
      "4389  09:45:24  17.43   0.01    1670  2910810   买盘\n",
      "4390  09:45:21  17.42   0.01     611  1064362   买盘\n",
      "4391  09:45:18  17.41  -0.01    1246  2169286   卖盘\n",
      "4392  09:45:15  17.42   0.01     802  1397084   买盘\n",
      "4393  09:45:12  17.41  -0.01    1465  2550565   卖盘\n",
      "4394  09:45:09  17.42   0.01    1093  1904006   买盘\n",
      "4395  09:45:06  17.41  -0.01     677  1178657   卖盘\n",
      "4396  09:45:03  17.42     --     728  1268176   买盘\n",
      "4397  09:45:00  17.42  -0.01    1021  1778582   卖盘\n",
      "4398  09:44:57  17.43     --     576  1003968   买盘\n",
      "4399  09:44:54  17.43     --    1247  2173521   卖盘\n",
      "4400  09:44:51  17.43  -0.01     541   942963   卖盘\n",
      "4401  09:44:48  17.44  -0.01     652  1137088   卖盘\n",
      "--------------------Data: 2017-06-15--------------------\n",
      "--------------------LowestPrice :15.9---------------------\n",
      "       time  price change  volume   amount type\n",
      "0  15:00:03  17.88     --    4647  8310051   卖盘\n",
      "1  14:57:00  17.88     --      52    92976   卖盘\n",
      "...... total: 4655\n",
      "--->从 09:30:03 到 09:30:51有合适的时间点：13个\n",
      "          time  price change  volume    amount type\n",
      "4636  09:30:51  15.95  -0.01     482    768790   卖盘\n",
      "4638  09:30:45  15.95   0.01     147    234465  中性盘\n",
      "4639  09:30:42  15.94   0.01    1207   1923958  中性盘\n",
      "4640  09:30:39  15.93     --     126    200718   卖盘\n",
      "4641  09:30:36  15.93     --     143    227799   卖盘\n",
      "4642  09:30:33  15.93     --     460    732780   卖盘\n",
      "4643  09:30:30  15.93  -0.03     118    187974   卖盘\n",
      "4645  09:30:24  15.92   0.01     262    417104   买盘\n",
      "4646  09:30:21  15.91  -0.02     104    165464   卖盘\n",
      "4647  09:30:18  15.93   0.01      88    140184   买盘\n",
      "4648  09:30:15  15.92   0.01     359    571528  中性盘\n",
      "4649  09:30:12  15.91  -0.05     315    501165   卖盘\n",
      "4652  09:30:03  15.95  -0.04    7060  11260700   卖盘\n",
      "--------------------Data: 2017-06-14--------------------\n",
      "--------------------LowestPrice :15.12---------------------\n",
      "       time  price change  volume    amount type\n",
      "0  15:00:03  16.25     --   11362  18463250  中性盘\n",
      "1  14:57:00  16.25   0.01     302    490750  中性盘\n",
      "...... total: 4636\n",
      "--->从 09:53:06 到 10:15:12有合适的时间点：155个\n",
      "          time  price change  volume   amount type\n",
      "3746  10:15:12  15.16     --      77   116732   卖盘\n",
      "3747  10:15:09  15.16     --      21    31836   卖盘\n",
      "3748  10:15:06  15.16  -0.01       7    10612   卖盘\n",
      "3751  10:14:57  15.16     --       8    12128   卖盘\n",
      "3752  10:14:54  15.16  -0.01      12    18192   卖盘\n",
      "3776  10:13:33  15.15  -0.01      20    30300   卖盘\n",
      "3777  10:13:30  15.16     --      17    26014   买盘\n",
      "3778  10:13:27  15.16  -0.04     686  1041249   卖盘\n",
      "3893  10:07:24  15.16  -0.01      60    90960   卖盘\n",
      "3895  10:07:18  15.16     --      14    21224   卖盘\n",
      "3896  10:07:15  15.16  -0.01      62    93992   卖盘\n",
      "3899  10:07:06  15.16  -0.01      21    31836   卖盘\n",
      "3901  10:07:00  15.16     --      21    31836   卖盘\n",
      "3902  10:06:57  15.16     --      14    21224   卖盘\n",
      "3903  10:06:54  15.16     --       3     4548   卖盘\n",
      "3904  10:06:51  15.16   0.01      16    24256   买盘\n",
      "3905  10:06:48  15.15  -0.01      79   119685   卖盘\n",
      "3906  10:06:45  15.16  -0.01      79   119764  中性盘\n",
      "3908  10:06:39  15.16  -0.01     185   280460   卖盘\n",
      "3910  10:06:33  15.16     --       2     3032   卖盘\n",
      "3911  10:06:30  15.16  -0.01      72   109152   卖盘\n",
      "3914  10:06:21  15.16  -0.01      45    68220   卖盘\n",
      "3917  10:06:12  15.16  -0.01      58    87928   卖盘\n",
      "3920  10:06:03  15.16  -0.01      66   100056   卖盘\n",
      "3998  10:01:57  15.16     --     128   194048   卖盘\n",
      "3999  10:01:54  15.16     --     114   172824   卖盘\n",
      "4000  10:01:51  15.16     --      55    83380   卖盘\n",
      "4001  10:01:48  15.16     --      60    90960   买盘\n",
      "4002  10:01:45  15.16     --     198   300168   买盘\n",
      "4003  10:01:42  15.16   0.01     543   823188   买盘\n",
      "...        ...    ...    ...     ...      ...  ...\n",
      "4144  09:54:39  15.14  -0.01      56    84784   卖盘\n",
      "4145  09:54:36  15.15   0.01     299   452985   买盘\n",
      "4146  09:54:33  15.14   0.01      57    86298   买盘\n",
      "4147  09:54:30  15.13  -0.01     120   181560   卖盘\n",
      "4148  09:54:27  15.14   0.01     191   289174   买盘\n",
      "4149  09:54:24  15.13  -0.01     177   267801   卖盘\n",
      "4150  09:54:21  15.14     --     170   257380   买盘\n",
      "4151  09:54:18  15.14   0.01     224   339136   买盘\n",
      "4152  09:54:15  15.13     --     180   272340   买盘\n",
      "4153  09:54:12  15.13     --     274   414562   买盘\n",
      "4154  09:54:09  15.13  -0.01     386   584018   卖盘\n",
      "4155  09:54:06  15.14   0.01     173   261922   买盘\n",
      "4156  09:54:03  15.13  -0.01     105   158865   卖盘\n",
      "4157  09:54:00  15.14     --      75   113550   买盘\n",
      "4158  09:53:57  15.14     --     180   272520   买盘\n",
      "4159  09:53:54  15.14     --     230   348220   卖盘\n",
      "4160  09:53:51  15.14  -0.01      41    62074   卖盘\n",
      "4161  09:53:48  15.15     --     231   349965   买盘\n",
      "4162  09:53:45  15.15     --     196   296940   买盘\n",
      "4163  09:53:42  15.15   0.01     177   268155   买盘\n",
      "4164  09:53:39  15.14  -0.01     144   218016   卖盘\n",
      "4165  09:53:36  15.15     --     176   266640   买盘\n",
      "4166  09:53:33  15.15     --     190   287850   买盘\n",
      "4167  09:53:30  15.15     --      58    87870   卖盘\n",
      "4168  09:53:27  15.15  -0.01     390   590850   卖盘\n",
      "4169  09:53:24  15.16     --      69   104604   卖盘\n",
      "4170  09:53:21  15.16     --     287   435092   买盘\n",
      "4171  09:53:18  15.16     --      58    87928   买盘\n",
      "4172  09:53:15  15.16  -0.01     350   530600   买盘\n",
      "4175  09:53:06  15.16  -0.03     225   341100   卖盘\n",
      "\n",
      "[155 rows x 6 columns]\n",
      "--------------------Data: 2017-06-07--------------------\n",
      "--------------------LowestPrice :14.86---------------------\n",
      "       time  price change  volume    amount type\n",
      "0  15:00:03  16.16     --   12825  20726412   卖盘\n",
      "1  14:57:00  16.16     --     231    373296   卖盘\n",
      "...... total: 2569\n",
      "df_moment is empty\n",
      "--------------------Data: 2017-06-01--------------------\n",
      "--------------------LowestPrice :15.68---------------------\n",
      "       time  price change  volume    amount type\n",
      "0  15:00:03  15.91   0.01    7448  11849768   买盘\n",
      "1  14:57:00  15.90  -0.01     153    243270   卖盘\n",
      "...... total: 4642\n",
      "--->从 09:30:30 到 09:30:30有合适的时间点：1个\n",
      "          time  price change  volume  amount type\n",
      "4630  09:30:30  15.71  -0.02     219  344049   卖盘\n",
      "--------------------Data: 2017-05-31--------------------\n",
      "--------------------LowestPrice :15.35---------------------\n",
      "       time  price change  volume   amount type\n",
      "0  15:00:03  15.56   0.01    4885  7601060   买盘\n",
      "1  14:57:00  15.55     --     185   287675   卖盘\n",
      "...... total: 4259\n",
      "--->从 09:39:45 到 09:40:36有合适的时间点：18个\n",
      "          time  price change  volume  amount type\n",
      "4045  09:40:36  15.39   0.01     126  193914   买盘\n",
      "4046  09:40:33  15.38     --      45   69210   卖盘\n",
      "4047  09:40:30  15.38   0.01     187  287606   买盘\n",
      "4048  09:40:27  15.37  -0.01      37   56869  中性盘\n",
      "4049  09:40:24  15.38   0.02      94  144572   买盘\n",
      "4050  09:40:21  15.36  -0.01      78  119808   卖盘\n",
      "4051  09:40:18  15.37   0.01      68  104516   买盘\n",
      "4052  09:40:15  15.36     --     439  674304   卖盘\n",
      "4053  09:40:12  15.36     --      96  147456   卖盘\n",
      "4054  09:40:09  15.36   0.01     205  314880   买盘\n",
      "4055  09:40:06  15.35  -0.01      77  118195   卖盘\n",
      "4056  09:40:03  15.36     --     248  380928   卖盘\n",
      "4057  09:40:00  15.36     --      24   36864   卖盘\n",
      "4058  09:39:57  15.36  -0.02      58   89088   卖盘\n",
      "4059  09:39:54  15.38     --     123  189174   买盘\n",
      "4060  09:39:51  15.38  -0.01      45   69210   卖盘\n",
      "4061  09:39:48  15.39     --      51   78489   买盘\n",
      "4062  09:39:45  15.39  -0.01     114  175446   卖盘\n",
      "--------------------Data: 2017-05-24--------------------\n",
      "--------------------LowestPrice :15.2---------------------\n",
      "       time  price change  volume    amount type\n",
      "0  15:00:03  15.82     --   10297  16290486   卖盘\n",
      "1  14:57:00  15.82  -0.01     113    178766   卖盘\n",
      "...... total: 4356\n",
      "--->从 09:38:42 到 09:39:00有合适的时间点：7个\n",
      "          time  price change  volume   amount type\n",
      "4174  09:39:00  15.24     --    1095  1668780   卖盘\n",
      "4175  09:38:57  15.24   0.01     771  1175004   买盘\n",
      "4176  09:38:54  15.23     --     770  1172710   买盘\n",
      "4177  09:38:51  15.23   0.01     120   182760   买盘\n",
      "4178  09:38:48  15.22  -0.02     198   301356   卖盘\n",
      "4179  09:38:45  15.24   0.01     266   405384  中性盘\n",
      "4180  09:38:42  15.23  -0.02     169   257387   卖盘\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--------------------Data: 2017-05-11--------------------\n",
      "--------------------LowestPrice :20.66---------------------\n",
      "       time  price change  volume    amount type\n",
      "0  15:00:03  22.73     --   17584  39968432   卖盘\n",
      "1  14:57:00  22.73  -0.01     351    797823   卖盘\n",
      "...... total: 4701\n",
      "--->从 11:27:18 到 13:07:18有合适的时间点：17个\n",
      "          time  price change  volume   amount type\n",
      "2193  13:07:18  20.70  -0.01     170   351900   卖盘\n",
      "2195  13:07:12  20.68     --     182   376376   卖盘\n",
      "2196  13:07:09  20.68     --     217   448756   卖盘\n",
      "2197  13:07:06  20.68     --     110   227480   买盘\n",
      "2198  13:07:03  20.68     --      37    76516   买盘\n",
      "2199  13:07:00  20.68  -0.02     314   649352   卖盘\n",
      "2200  13:06:57  20.70  -0.02      59   122130   卖盘\n",
      "2380  11:27:51  20.70  -0.01     211   436770   卖盘\n",
      "2383  11:27:42  20.68   0.01     480   992640  中性盘\n",
      "2384  11:27:39  20.67     --     328   677976   卖盘\n",
      "2385  11:27:36  20.67     --     497  1027299   卖盘\n",
      "2386  11:27:33  20.67   0.01     668  1380756   卖盘\n",
      "2387  11:27:30  20.66  -0.01     532  1099112   卖盘\n",
      "2388  11:27:27  20.67     --     762  1575054   买盘\n",
      "2389  11:27:24  20.67  -0.03     169   349323   卖盘\n",
      "2390  11:27:21  20.70     --     690  1428300   买盘\n",
      "2391  11:27:18  20.70  -0.02     791  1637370   卖盘\n",
      "--------------------Data: 2017-05-09--------------------\n",
      "--------------------LowestPrice :18.46---------------------\n",
      "       time  price change  volume    amount type\n",
      "0  15:00:03  22.30   0.02   25203  56202690   卖盘\n",
      "1  14:57:00  22.28     --     557   1240996   买盘\n",
      "...... total: 4640\n",
      "--->从 10:47:54 到 10:48:21有合适的时间点：5个\n",
      "          time  price change  volume  amount type\n",
      "3072  10:48:21   18.5  -0.01     431  797350   卖盘\n",
      "3074  10:48:15   18.5     --     424  784400   买盘\n",
      "3075  10:48:12   18.5  -0.03     317  587745   卖盘\n",
      "3078  10:48:03   18.5  -0.03     259  479150   卖盘\n",
      "3081  10:47:54   18.5  -0.01     145  268250   卖盘\n",
      "--------------------Data: 2017-05-03--------------------\n",
      "--------------------LowestPrice :21.23---------------------\n",
      "       time  price change  volume    amount type\n",
      "0  15:00:03  22.88  -0.02   22657  51839216  中性盘\n",
      "1  14:57:00  22.90   0.01     350    801500   买盘\n",
      "...... total: 4631\n",
      "--->从 09:30:39 到 09:30:48有合适的时间点：3个\n",
      "          time  price change  volume   amount type\n",
      "4613  09:30:48  21.23  -0.05     913  1938299   卖盘\n",
      "4615  09:30:42  21.23  -0.04     513  1089099   卖盘\n",
      "4616  09:30:39  21.27  -0.03     617  1312359   卖盘\n",
      "--------------------Data: 2017-05-02--------------------\n",
      "--------------------LowestPrice :19.4---------------------\n",
      "       time  price change  volume   amount type\n",
      "0  15:00:03  21.33     --    2357  5027481   卖盘\n",
      "1  14:57:00  21.33     --     134   285822   卖盘\n",
      "...... total: 4659\n",
      "--->从 09:30:03 到 09:30:09有合适的时间点：3个\n",
      "          time  price change  volume    amount type\n",
      "4654  09:30:09   19.4     --     653   1266820   卖盘\n",
      "4655  09:30:06   19.4     --    1426   2766440   卖盘\n",
      "4656  09:30:03   19.4  -0.08    7898  15322119   卖盘\n"
     ]
    }
   ],
   "source": [
    "\n",
    "def get_low_price_moment(stock_code, df_stock_operate):\n",
    "    for i_date in df_stock_operate.index:\n",
    "        \n",
    "        #获取某一天的历史价格变动情况，好像间隔周期是3秒\n",
    "        df_tick_data = ts.get_tick_data(stock_code,date=i_date) \n",
    "        lowPrice = float(df_stock_operate['low'][i_date])\n",
    "        print('--------------------Data: '+ i_date+'--------------------')\n",
    "        print('--------------------LowestPrice :'+str(lowPrice)+'---------------------')\n",
    "        print(df_tick_data.head(2))\n",
    "        print('...... total: '+str(len(df_tick_data)))\n",
    "        \n",
    "        df_moment = df_tick_data[\n",
    "            (df_tick_data['price']-lowPrice < 0.05)\n",
    "        ]\n",
    "        size = len(df_moment)\n",
    "        if size != 0:\n",
    "            #print('df_moment.size = '+str(size))\n",
    "            #print(df_moment.iloc[0,0]) #iloc[column, row]，不需要知道行号和列号，直接获取单位元素\n",
    "            #print(df_moment.iloc[size-1,0])\n",
    "            datetimeEnd = df_moment.iloc[0,0]\n",
    "            datatimeStart = df_moment.iloc[size-1,0]\n",
    "            print('--->从 '+datatimeStart+ ' 到 '+ datetimeEnd + '有合适的时间点：'+str(size)+'个')\n",
    "            print(df_moment)\n",
    "        else:\n",
    "            print('df_moment is empty')\n",
    "        \n",
    "get_low_price_moment('002307',df_stock_operate4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>time</th>\n",
       "      <th>price</th>\n",
       "      <th>change</th>\n",
       "      <th>volume</th>\n",
       "      <th>amount</th>\n",
       "      <th>type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>alert(\"当天没有数据\");</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>window.close();</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>&lt;/script&gt;</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               time  price  change  volume  amount  type\n",
       "0  alert(\"当天没有数据\");    NaN     NaN     NaN     NaN   NaN\n",
       "1   window.close();    NaN     NaN     NaN     NaN   NaN\n",
       "2         </script>    NaN     NaN     NaN     NaN   NaN"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
